package org.jeecg.modules.qy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.qy.entity.QyCompany;
import org.jeecg.modules.qy.service.IQyCompanyService;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 公司信息表
 * @Author: jeecg-boot
 * @Date:   2021-03-05
 * @Version: V1.0
 */
@Api(tags="公司信息表")
@RestController
@RequestMapping("/qy/qyCompany")
@Slf4j
public class QyCompanyController extends JeecgController<QyCompany, IQyCompanyService> {
	@Autowired
	private IQyCompanyService qyCompanyService;

	 @Autowired
	 private ISysUserRoleService sysUserRoleService;
	 @Autowired
	 private ISysRoleService roleService;
	/**
	 * 分页列表查询
	 *
	 * @param qyCompany
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "公司信息表-分页列表查询")
	@ApiOperation(value="公司信息表-分页列表查询", notes="公司信息表-分页列表查询")
	@PermissionData(pageComponent = "qyCompany/list")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(QyCompany qyCompany,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<QyCompany> queryWrapper = QueryGenerator.initQueryWrapper(qyCompany, req.getParameterMap());
		Page<QyCompany> page = new Page<QyCompany>(pageNo, pageSize);
		IPage<QyCompany> pageList = qyCompanyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param qyCompany
	 * @return
	 */
	@AutoLog(value = "公司信息表-添加")
	@ApiOperation(value="公司信息表-添加", notes="公司信息表-添加")
	@RequiresPermissions("qyCompany:add")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody QyCompany qyCompany) {
		qyCompanyService.saveCompany(qyCompany);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param qyCompany
	 * @return
	 */
	@AutoLog(value = "公司信息表-编辑")
	@ApiOperation(value="公司信息表-编辑", notes="公司信息表-编辑")
	@RequiresPermissions("qyCompany:edit")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody QyCompany qyCompany) {
		qyCompanyService.updateById(qyCompany);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "公司信息表-通过id删除")
	@ApiOperation(value="公司信息表-通过id删除", notes="公司信息表-通过id删除")
	@RequiresPermissions("qyCompany:delete")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		qyCompanyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "公司信息表-批量删除")
	@ApiOperation(value="公司信息表-批量删除", notes="公司信息表-批量删除")
	@RequiresPermissions("qyCompany:delete")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.qyCompanyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "公司信息表-通过id查询")
	@ApiOperation(value="公司信息表-通过id查询", notes="公司信息表-通过id查询")
	@RequiresPermissions("qyCompany:query")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		QyCompany qyCompany = qyCompanyService.getById(id);
		if(qyCompany==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(qyCompany);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param qyCompany
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("qyCompany:expert")
    public ModelAndView exportXls(HttpServletRequest request, QyCompany qyCompany) {
        return super.exportXls(request, qyCompany, QyCompany.class, "公司信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@RequiresPermissions("qyCompany:import")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, QyCompany.class);
    }

	/**
	 * 查询所有公司列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findCompanyList", method = RequestMethod.POST)
    public Result<List<QyCompany>> findCompanyList(HttpServletRequest request, HttpServletResponse response){
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //查询角色
        List<SysUserRole> userRoleList = sysUserRoleService.list(new QueryWrapper<SysUserRole>().eq("user_id",user.getId()));
        if(CollectionUtils.isNotEmpty(userRoleList)){
            boolean hasRole= false;
            for(SysUserRole userRole:userRoleList){

                SysRole role = roleService.getById(userRole.getRoleId());

                if(role!=null&&"smsy_admin".equals(role.getDescription())){
                    hasRole = true;

                }
            }

            if(hasRole){
                return Result.OK(qyCompanyService.list(new QueryWrapper<QyCompany>().orderByDesc("create_time")));
            }
        }
		return Result.OK();
	}

}
